﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmPhieuTraSach : Form
    {
        public frmPhieuTraSach()
        {
            InitializeComponent();
            dgvPhieuTraSach.Columns.Add("MASACH", "Mã sách");
            dgvPhieuTraSach.Columns.Add("MAPMS", "Mã PMS");
            frmQuyDinh.laygiatriquydinh();
        }
       
        SachControl scontrol = new SachControl();
        SachInfo sinfo = new SachInfo();

        CT_PTScontrol ctptsctrl = new CT_PTScontrol();
        PTScontrol ptsctrl = new PTScontrol();
        DausachControl dscontrol = new DausachControl();
        CTPMcontrol ctpmctrl = new CTPMcontrol();
        NhanvienControl nvcontrol = new NhanvienControl();
        PhieumuonControl pmcontrol = new PhieumuonControl();
        DocgiaControl dgcontrol = new DocgiaControl();
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        DataTable dt3 = new DataTable();
        DataTable dt4 = new DataTable();
        DataTable dt5 = new DataTable();
        DataTable dt6 = new DataTable();
        DataTable dt7 = new DataTable();
        string[] temp= new string[10];
        int[] sosachden = new int[10];
        int tongtrigia = 0;
        PTScontrol tsctrl = new PTScontrol();
        public static string mapmstemp, masachtemp,masachtemp2,madg1;
        public static int trigiapts,trigiapts2;
        bool kt=true;
        bool check;
        int z;
        public void btnThem_Click(object sender, EventArgs e)
        {
            lblMaDocGia.Visible = true;
            lblMaPhieuTra.Visible = true;
            lblMaThuThu.Visible = true;
            lblNgayTra.Visible = true;
            lblPhiTraSach.Visible = true;
            TimeSpan ts;
            for (z = dgvPhieuTraSach.Rows.Count; z <= dgvPhieuTraSach.Rows.Count; z++)
            {
                if (txtNhapMaSach.Text == "")
                {
                    MessageBox.Show(" Vui Lòng Nhập Mã Sách");
                }

                else
                {
                    CTPMinfo pminfo = new CTPMinfo();
                    pminfo.Masach = txtNhapMaSach.Text;
                    dt = ctpmctrl.layctpms(pminfo);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("Mã Sách Này Không Tồn Tại Hoặc Sách Chưa Được Mượn");
                    }
                    else
                    {
                        if (cbxLoaiPhi.SelectedItem == null)
                        {
                            MessageBox.Show("Vui Lòng Chọn Loại Phí");
                        }
                        else
                        {
                            DataRow dr = dt.Rows[0];
                            dgvPhieuTraSach.Rows.Add(dr["MASACH"].ToString(), dr["MAPMS"].ToString());
                            temp[dgvPhieuTraSach.Rows.Count - 1] = cbxLoaiPhi.SelectedItem.ToString();
                            mapmstemp = dr["MAPMS"].ToString();

                            lblMaPhieuTra.Text = dr["MAPMS"].ToString();
                            //lblMaPhieuTra.Enabled = true;
                            lblNgayTra.Text = DateTime.Today.ToShortDateString();
                            //lblNgayTra.Enabled = true;

                            dt = nvcontrol.NhanvienDangnhap();
                            dr = dt.Rows[0];
                            lblMaThuThu.Text = dr["MANV"].ToString();
                            //lblMaThuThu.Enabled = true;

                            dt = pmcontrol.madgpts();
                            dr = dt.Rows[0];
                            lblMaDocGia.Text = dr["MADG"].ToString();
                            //lblMaDocGia.Enabled = true;
                            if (cbxLoaiPhi.SelectedItem != null)
                            {
                                if (cbxLoaiPhi.SelectedItem == "Muon sach")
                                {
                                    dt3 = pmcontrol.ngaymuon();
                                    dr = dt3.Rows[0];
                                    DateTime ngaymuon = Convert.ToDateTime(dr["NGMUON"].ToString());
                                    ts = DateTime.Now - ngaymuon;
                                    int songay= Convert.ToInt16(ts.TotalDays);
                                    if (songay <= 0)
                                    {
                                        lblPhiTraSach.Text = "0";
                                    }
                                    else

                                        lblPhiTraSach.Text = (songay * frmQuyDinh.sotienmoingay).ToString();
                                }
                                if (cbxLoaiPhi.SelectedItem == "Den sach")
                                {
                                    masachtemp = dgvPhieuTraSach.Rows[z].Cells["MASACH"].Value.ToString();
                                    dt3 = dscontrol.laytrigia();
                                    dr = dt3.Rows[0];
                                    lblPhiTraSach.Text = dr["TRIGIA"].ToString();
                                    //lblPhiTraSach.Enabled = true;

                                }
                            }
                            masachtemp = txtNhapMaSach.Text;
                            if (rdbHay.Checked == true)
                            {
                                dt7 = scontrol.layvote();
                                dr = dt7.Rows[0];
                                sinfo.Hay = Convert.ToInt16(dr["THICH"].ToString()) + 1;
                                sinfo.Madausach = dr["MADAUSACH"].ToString();
                                scontrol.updatehay(sinfo);
                            }

                        }
                    }
                }
                z++;
            }
        }

        private void frmPhieuTraSach_Load(object sender, EventArgs e)
        {
            
        }

        private void laythongtinpts(ref PTSinfo info)
        {
            TimeSpan ts;

            dt = nvcontrol.NhanvienDangnhap();
            DataRow dr = dt.Rows[0];
            info.Manv = dr["MANV"].ToString();
            dt2 = pmcontrol.madgpts();
            if (dt2.Rows.Count == 0)
            {
                MessageBox.Show("Chưa Có Sách Trả");
                kt = false;
            }
            else
            {
                dr = dt2.Rows[0];
                info.Madg = dr["MADG"].ToString();
                info.Ngtra = DateTime.Today;
                dt3 = pmcontrol.ngaymuon();
                dr = dt3.Rows[0];
                DateTime ngaymuon = Convert.ToDateTime(dr["NGMUON"].ToString());
                ts = DateTime.Now - ngaymuon;
                int ngaytinhtratien = Convert.ToInt16(ts.TotalDays);
                int sosachmuon = 0;
                int i = 0;
                while (temp[i] == "Muon sach")
                {
                    sosachmuon = sosachmuon + 1;
                    i++;
                }

                tongtrigia = sosachmuon * frmQuyDinh.sotienmoingay * ngaytinhtratien;
                for (int j = 0; j < dgvPhieuTraSach.Rows.Count; j++)
                {
                    if (temp[j] == "Den sach")
                    {
                        sosachden[j] = 1;
                        j++;
                    }
                }
                for (int n = 0; n < dgvPhieuTraSach.Rows.Count; n++)
                {
                    if (sosachden[n] != 1)
                    {
                        continue;
                    }
                    else
                    {
                        masachtemp = dgvPhieuTraSach.Rows[n].Cells["MASACH"].Value.ToString();
                        dt4 = dscontrol.laytrigia();
                        dr = dt4.Rows[0];
                        int tempprice = Convert.ToInt32(dr["TRIGIA"].ToString());
                        tongtrigia = tongtrigia + tempprice;
                        //n++;

                    }
                }
                info.Trigiapts = tongtrigia;
                trigiapts = tongtrigia;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            
            PTSinfo tinfo = new PTSinfo();
            CT_PTSinfo cinfo = new CT_PTSinfo();
            laythongtinpts(ref tinfo);
            if (kt == true)
            {
                madg1 = tinfo.Madg;
                ptsctrl.insertPTS(tinfo);
                for (int i = 0; i < dgvPhieuTraSach.RowCount ; i++)
                {
                    sinfo.Masach = dgvPhieuTraSach.Rows[i].Cells["MASACH"].Value.ToString();
                    scontrol.updatetrasach(sinfo);
                    cinfo.Ngtra = DateTime.Today;
                    cinfo.Masach = dgvPhieuTraSach.Rows[i].Cells["MASACH"].Value.ToString();
                    cinfo.Loaiphi = temp[i];
                    dt5 = ptsctrl.laymapts();
                    DataRow dr = dt5.Rows[0];
                    cinfo.Mapts = dr["MAPTS"].ToString();
                    ctptsctrl.insertCTPTS(cinfo);
                }

                //dt6 = dgcontrol.Laysotienno();
                //DataRow dr13 = dt6.Rows[0];
                //trigiapts2 = Convert.ToInt32(dr13["SOTIENNO"].ToString()) + tongtrigia;
                //dgcontrol.updatestn();
                frmChinh.check = true;
                frmTraTien frm = new frmTraTien();
                frm.ShowDialog();
                this.Close();
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }      
    }
}
